
const util = require("../../utils/util.js");
const app = getApp();

// pages/today/today.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    massage:"",
    day:'',
    what: '安全期',
    msg:'',
    diIf:false,
    p2s:true,
    m:'',
    d:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var cyc = app.globalData.setting.cycle;
    var sessionId = app.globalData.sessionId

    if (app.globalData.isFirst2) {
      app.globalData.isFirst = true;
    } else {
      app.globalData.isFirst = false;
    }

    loadToday();
  
    function loadToday(){
      wx.request({
        url: "https://wx.woqutech.cn/Mobile/Menstruation/records?&sessionId=" + sessionId,
        success: function (res) {
          console.log(res.data);
          if (res.data.data.length == 0) {
            that.setData({
              massage: '经期未记录',
              p2s: false,
              what: '未知期',
              msg: '',
            })
          } else {
            that.setData({
              p2s: true
            })

            var toD = util.formatTime(new Date).replace(new RegExp("/", "g"), '-');
            var compare = function (a, b) {
              return (new Date(a) > new Date(b))
            };
            var xc = function (d1, d2) {
              var d4 = new Date(d1.replace(/-/g, "/"));
              var d5 = new Date(d2.replace(/-/g, "/"));
              var da = d4.getTime() - d5.getTime();
              return parseInt(da / (1000 * 60 * 60 * 24))
            };
            var addZero = function (a) {
              if (a < 10) {
                return '0' + a
              } else {
                return a
              }
            };
            var addDate = function (date, days) {
              var d = new Date(date);
              d.setDate(d.getDate() + days);
              var m = d.getMonth() + 1;
              return d.getFullYear() + '-' + addZero(m) + '-' + addZero(d.getDate());
            };

            var dArr = [];
            var aqArr = [];
            var bgDate = res.data.latest.beginDate;
            var days = res.data.latest.days;

            for (var i = 0; i < days; i++) {
              dArr.push(addDate(bgDate, i));
              if (toD == addDate(bgDate, i)) {   //情况已1  如果今天在月经期内
                that.setData({
                  massage: '今天是经期',
                  day: xc(toD, bgDate) + 1,
                  diIf: true,
                  what: '月经中',
                  msg: '预期经期结束时间',
                })
              }
            };

            that.setData({
              msg: '预期经期结束时间',
              m: Number(dArr[dArr.length - 1].split('-')[1]),
              d: Number(dArr[dArr.length - 1].split('-')[2])
            });
            //console.log(xc(addDate(dArr[0], Number(cyc)), toD))  //预测 - 今天
            //情况已2 月经在今天之前
            

            if (compare(toD, dArr[dArr.length - 1])) {
              if (cyc > 19) {
                var aqbg = addDate(dArr[0], cyc - 19)
                var ds = 0
                for (var i = 0; i < 10; i++) {
                  if (addDate(aqbg, i) == toD) {
                    that.setData({
                      what: '排卵期',
                    });
                  };
                  ds++
                  // else if {
                  //   that.setData({
                  //     what: '安全期',
                  //   });
                  // }
                  aqArr.push(addDate(aqbg, i))
                };

                if (compare(toD, addDate(aqbg, ds - 1)) || compare(addDate(aqbg, 0), toD)){
                  that.setData({
                      what: '安全期',
                  });
                }

              } else {
                for (var i = 0; i < 10 - 20 - cyc; i++) {
                  if (addDate(aqbg, i) == toD) {
                    that.setData({
                      what: '排卵期',
                    });
                  } else {
                    that.setData({
                      what: '安全期',
                    });
                  }
                  aqArr.push(addDate(aqbg, i))
                }
              };
              // console.log(aqArr)
          
              if (xc(addDate(dArr[0], Number(cyc)), toD) < 0) {
                that.setData({
                  massage: '大姨妈推迟了',
                  diIf: false,
                  day: Math.abs(xc(addDate(dArr[0], Number(cyc)), toD)),
                  msg: '预期下次经期开始时间',
                  what: '安全期',
                  m: Number(toD.split('-')[1]),
                  d: Number(toD.split('-')[2])
                })
              } else {

                that.setData({
                  massage: '距离下次大姨妈还有',
                  diIf: false,
                  day: xc(addDate(dArr[0], Number(cyc)), toD),
                  msg: '预期下次经期开始时间',
                  m: Number(addDate(dArr[0], Number(cyc)).split('-')[1]),
                  d: Number(addDate(dArr[0], Number(cyc)).split('-')[2])
                })
              }
            }
          }
        }
      }) 
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})